﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Configuration;
using Insifo.BE.Gen;

namespace Insifo.DL.Gen
{
    public class DLCompania : DLTransaction, IDLTransaccion<BECompania>
    {
        #region  Members
         

        /// <summary>
        /// Método obtiene obtiene la Lista las Companias
        /// </summary>
        /// <returns>Devuelve un DataSet</returns>
        public DataSet Listar(BECompania pbe)
        {
            DataSet ds = new DataSet();
            //List<BECompania> lst = new List<BECompania>();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Listar");
            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;

                
                sqlDB.AddInParameter(dbCmd, "@as_CodigoCompania", DbType.String, pbe.CodigoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Descripcion", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                sqlDB.AddInParameter(dbCmd, "@ai_Pagina", DbType.Int32, pbe.Pagina);
                ds = sqlDB.ExecuteDataSet(dbCmd);


            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                dbCmd.Dispose();
            }
            return ds;
        }

        /// <summary>
        /// Método obtiene las Companias
        /// </summary>
        /// <returns>Devuelve un DataSet</returns>
        public DataSet Listar_Sel(BECompania pbe)
        {
            DataSet ds = new DataSet();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Listar_Sel");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
                sqlDB.AddInParameter(dbCmd, "@as_CodigoCompania", DbType.String, pbe.CodigoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Descripcion", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                ds = sqlDB.ExecuteDataSet(dbCmd);
            }
            catch (Exception)
            {
                throw;
            }

            finally
            {                
                //if (dbCmd.Connection.State == ConnectionState.Open)
                    //dbCmd.Connection.Close();
                dbCmd.Dispose();
            }
            return ds;
        }

        /// <summary>
        /// Método obtiene las Companias
        /// </summary>
        /// <returns>Devuelve un DataSet</returns>
        public DataSet ListarPorUsuario(string pstrUsuario)
        {
            DataSet ds = new DataSet();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_X_Usuario_Listar");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
                sqlDB.AddInParameter(dbCmd, "@as_CodigoUsuario", DbType.String, pstrUsuario);
                ds = sqlDB.ExecuteDataSet(dbCmd);
            }
            catch (Exception)
            {
                throw;
            }

            finally
            {
                //if (dbCmd.Connection.State == ConnectionState.Open)
                //dbCmd.Connection.Close();
                dbCmd.Dispose();
            }
            return ds;
        }

        /// <summary>
        /// Método obtiene una Compania
        /// </summary>
        /// <returns>Devuelve un objeto</returns>
        public BECompania Obtener(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Obtener");
            dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
            try
            {
                sqlDB.AddInParameter(dbCmd, "@ai_IdCompania", DbType.String, pbe.IdCompania);

                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<int>(reader, "IdCompania");
                        be.IdPersona = DBValue.Get<int>(reader, "IdPersona");
                        be.CodigoCompania = DBValue.Get<String>(reader, "CodigoCompania");
                        be.Descripcion = DBValue.Get<String>(reader, "Descripcion");
                        be.PaginaWeb = DBValue.Get<String>(reader, "PaginaWeb");
                        be.AfectoIGV = DBValue.Get<String>(reader, "AfectoIGV");
                        be.RetencionIGV = DBValue.Get<String>(reader, "RetencionIGV");
                        be.CreditoFiscal = DBValue.Get<String>(reader, "CreditoFiscal");
                        be.RTPS = DBValue.Get<String>(reader, "RTPS");
                        be.Logo = DBValue.Get<String>(reader, "Logo");
                        be.RepresentanteLegal = DBValue.Get<String>(reader, "RepresentanteLegal");
                        be.RepresentanteLegalDocumento = DBValue.Get<String>(reader, "RepresentanteLegalDocumento");
                        be.TipoCompania = DBValue.Get<String>(reader, "TipoCompania");
                        be.RangoIngresos = DBValue.Get<String>(reader, "RangoIngresos");
                        be.RangoPersonal = DBValue.Get<String>(reader, "RangoPersonal");
                        be.General = DBValue.Get<String>(reader, "General");
                        be.IdPersonaLegal = DBValue.Get<Int32>(reader, "IdPersonaLegal");
                        be.IdPaisRegistroPersona = DBValue.Get<Int32>(reader, "IdPaisRegistroPersona");
                        be.IdDepartamentoRegistroPersona = DBValue.Get<Int32>(reader, "IdDepartamentoRegistroPersona");
                        be.IdProvinciaRegistroPersona = DBValue.Get<Int32>(reader, "IdProvinciaRegistroPersona");
                        be.FechaRegistroPersona = DBValue.Get<DateTime>(reader, "FechaRegistroPersona") == DateTime.MinValue ? (DateTime?)null : DBValue.Get<DateTime>(reader, "FechaRegistroPersona");
                        be.TomoRegistroPersona = DBValue.Get<string>(reader, "TomoRegistroPersona");
                        be.FechaRegistro = DBValue.Get<DateTime>(reader, "FechaRegistro") == DateTime.MinValue ? (DateTime?)null : DBValue.Get<DateTime>(reader, "FechaRegistro");
                        be.IdPaisRegistro = DBValue.Get<Int32>(reader, "IdPaisRegistro");
                        be.IdDepartamentoRegistro = DBValue.Get<Int32>(reader, "IdDepartamentoRegistro");
                        be.IdProvinciaRegistro = DBValue.Get<Int32>(reader, "IdProvinciaRegistro");
                        be.TomoRegistroLegal = DBValue.Get<string>(reader, "TomoRegistroLegal");
                        be.Nemonico = DBValue.Get<string>(reader, "Nemonico");

                        be.Estado = DBValue.Get<String>(reader, "Estado");
                        be.UsuarioModificacion = DBValue.Get<String>(reader, "UsuarioModificacion");
                        be.FechaModificacion = DBValue.Get<DateTime>(reader, "FechaModificacion");
                        be.UsuarioCreacion = DBValue.Get<String>(reader, "UsuarioCreacion");
                        be.FechaCreacion = DBValue.Get<DateTime>(reader, "FechaCreacion");

                        //Foto del Empleado
                        object objImagenLogo = reader["ImagenLogo"];
                        if (objImagenLogo is byte[])
                            be.ImagenLogo = (byte[])objImagenLogo;
                        break;
                        
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                //if (dbCmd.Connection.State == ConnectionState.Open)
                //dbCmd.Connection.Close();
                dbCmd.Dispose();
            }
            return be;
        }

        /// <summary>
        /// Método Inserta
        /// </summary>
        /// <returns>Devuelve una entidad</returns>
        public BECompania Insertar(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Insertar");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
                sqlDB.AddInParameter(dbCmd, "@ai_IdPersona", DbType.Int32, pbe.IdPersona);
                sqlDB.AddInParameter(dbCmd, "@as_CodigoCompania", DbType.String, pbe.CodigoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Descripcion", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_PaginaWeb", DbType.String, pbe.PaginaWeb);
                sqlDB.AddInParameter(dbCmd, "@as_AfectoIGV", DbType.String, pbe.AfectoIGV);
                sqlDB.AddInParameter(dbCmd, "@as_RetencionIGV", DbType.String, pbe.RetencionIGV);
                sqlDB.AddInParameter(dbCmd, "@as_CreditoFiscal", DbType.String, pbe.CreditoFiscal);
                sqlDB.AddInParameter(dbCmd, "@as_RTPS", DbType.String, pbe.RTPS);
                sqlDB.AddInParameter(dbCmd, "@as_Logo", DbType.String, pbe.Logo);
                sqlDB.AddInParameter(dbCmd, "@as_RepresentanteLegal", DbType.String, pbe.RepresentanteLegal);
                sqlDB.AddInParameter(dbCmd, "@as_RepresentanteLegalDocumento", DbType.String, pbe.RepresentanteLegalDocumento);
                sqlDB.AddInParameter(dbCmd, "@as_TipoCompania", DbType.String, pbe.TipoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_RangoIngresos", DbType.String, pbe.RangoIngresos);
                sqlDB.AddInParameter(dbCmd, "@as_RangoPersonal", DbType.String, pbe.RangoPersonal);
                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                sqlDB.AddInParameter(dbCmd, "@as_UsuarioCreacion", DbType.String, pbe.UsuarioCreacion);
                sqlDB.AddInParameter(dbCmd, "@as_General", DbType.String, pbe.General);
                sqlDB.AddInParameter(dbCmd, "@ai_IdPersonaLegal", DbType.Int32, pbe.IdPersonaLegal);
                sqlDB.AddInParameter(dbCmd, "@ai_IdPaisRegistroPersona", DbType.Int32, pbe.IdPaisRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@ai_IdDepartamentoRegistroPersona", DbType.Int32, pbe.IdDepartamentoRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@ai_IdProvinciaRegistroPersona", DbType.Int32, pbe.IdProvinciaRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@at_FechaRegistroPersona", DbType.DateTime, pbe.FechaRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@as_TomoRegistroPersona", DbType.String, pbe.TomoRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@at_FechaRegistro", DbType.DateTime, pbe.FechaRegistro);
                sqlDB.AddInParameter(dbCmd, "@ai_IdPaisRegistro", DbType.Int32, pbe.IdPaisRegistro);
                sqlDB.AddInParameter(dbCmd, "@ai_IdDepartamentoRegistro", DbType.Int32, pbe.IdDepartamentoRegistro);
                sqlDB.AddInParameter(dbCmd, "@ai_IdProvinciaRegistro", DbType.Int32, pbe.IdProvinciaRegistro);
                sqlDB.AddInParameter(dbCmd, "@as_TomoRegistroLegal", DbType.String, pbe.TomoRegistroLegal);
                sqlDB.AddInParameter(dbCmd, "@as_Nemonico", DbType.String, pbe.Nemonico);
                sqlDB.AddInParameter(dbCmd, "@aim_ImagenLogo", DbType.Binary, pbe.ImagenLogo);
 
                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<Int32>(reader, "IdCompania");
                        be.FechaCreacion = DBValue.Get<DateTime>(reader, "FechaCreacion");
                        //Error
                        be.Error = DBValue.Get<Int32>(reader, "CodigoError");
                        be.Mensaje = DBValue.Get<String>(reader, "MensajeError");

                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            finally
            {
                dbCmd.Dispose();
            }
            return be;
        }

        /// <summary>
        /// Método Inserta Spring
        /// </summary>
        /// <returns>Devuelve una entidad</returns>
        public BECompania InsertarSpring(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Insertar_Spring");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;

                sqlDB.AddInParameter(dbCmd, "@ai_IdCompania", DbType.Int32, pbe.IdCompania);
                sqlDB.AddInParameter(dbCmd, "@ai_IdPersona", DbType.Int32, pbe.IdSpring);
                sqlDB.AddInParameter(dbCmd, "@as_CodigoCompania", DbType.String, pbe.CodigoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Descripcion", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_DescripcionLarga", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_PaginaWeb", DbType.String, pbe.PaginaWeb);
                sqlDB.AddInParameter(dbCmd, "@as_AfectoIGV", DbType.String, pbe.AfectoIGV);
                sqlDB.AddInParameter(dbCmd, "@as_RetencionIGV", DbType.String, pbe.RetencionIGV);
                sqlDB.AddInParameter(dbCmd, "@as_CreditoFiscal", DbType.String, pbe.CreditoFiscal);
                sqlDB.AddInParameter(dbCmd, "@as_RTPS", DbType.String, pbe.RTPS);
                sqlDB.AddInParameter(dbCmd, "@as_RepresentanteLegal", DbType.String, pbe.RepresentanteLegal);
                sqlDB.AddInParameter(dbCmd, "@as_RepresentanteLegalDocumento", DbType.String, pbe.RepresentanteLegalDocumento);
                sqlDB.AddInParameter(dbCmd, "@as_TipoEmpresa", DbType.String, pbe.TipoCompania);
                sqlDB.AddInParameter(dbCmd, "@at_FechaRegistro", DbType.DateTime, pbe.FechaNacimiento);
                sqlDB.AddInParameter(dbCmd, "@as_TomoRegistroLegal", DbType.String, pbe.TomoRegistroLegal);
                sqlDB.AddInParameter(dbCmd, "@as_Direccion", DbType.String, pbe.DescripcionDireccion );
                sqlDB.AddInParameter(dbCmd, "@as_DireccionReferencia", DbType.String, pbe.DireccionReferencia);
                sqlDB.AddInParameter(dbCmd, "@as_Documento", DbType.String, pbe.Documento);
                sqlDB.AddInParameter(dbCmd, "@as_Telefono1", DbType.String, pbe.Telefono1);
                sqlDB.AddInParameter(dbCmd, "@as_Telefono2", DbType.String, pbe.Telefono2);
                sqlDB.AddInParameter(dbCmd, "@as_Telefono3", DbType.String, pbe.Telefono3);
                sqlDB.AddInParameter(dbCmd, "@as_Fax1", DbType.String, pbe.Fax);
                sqlDB.AddInParameter(dbCmd, "@as_Nacionalidad", DbType.String, pbe.Nacionalidad);
                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                sqlDB.AddInParameter(dbCmd, "@as_UsuarioCreacion", DbType.String, pbe.UsuarioCreacion);
                
                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<Int32>(reader, "IdCompania");
                        be.FechaCreacion = DBValue.Get<DateTime>(reader, "FechaCreacion");
                        //Error
                        be.Error = DBValue.Get<Int32>(reader, "CodigoError");
                        be.Mensaje = DBValue.Get<String>(reader, "MensajeError");

                        break;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                dbCmd.Dispose();
            }
            return be;
        }

        /// <summary>
        /// Método Inserta tabla Owner.
        /// </summary>
        /// <returns>Devuelve una entidad</returns>
        public BECompania InsertarOwnerSpring(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Insertar_Owner_Spring");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
                sqlDB.AddInParameter(dbCmd, "@ai_IdCompania", DbType.Int32, pbe.IdCompania);
                sqlDB.AddInParameter(dbCmd, "@as_CodigoCompania", DbType.String, pbe.CodigoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Descripcion", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_UsuarioCreacion", DbType.String, pbe.UsuarioCreacion);
                
                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<Int32>(reader, "IdCompania");
                        be.FechaCreacion = DBValue.Get<DateTime>(reader, "FechaCreacion");
                        //Error
                        be.Error = DBValue.Get<Int32>(reader, "CodigoError");
                        be.Mensaje = DBValue.Get<String>(reader, "MensajeError");

                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            finally
            {
                dbCmd.Dispose();
            }
            return be;
        }


        /// <summary>
        /// Método Actualiza 
        /// </summary>
        /// <returns>Devuelve una entidad</returns>
        public BECompania Actualizar(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Actualizar");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;

                sqlDB.AddInParameter(dbCmd, "@ai_IdCompania", DbType.Int32, pbe.IdCompania);
                sqlDB.AddInParameter(dbCmd, "@ai_IdPersona", DbType.Int32, pbe.IdPersona);
                sqlDB.AddInParameter(dbCmd, "@as_CodigoCompania", DbType.String, pbe.CodigoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Descripcion", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_PaginaWeb", DbType.String, pbe.PaginaWeb);
                sqlDB.AddInParameter(dbCmd, "@as_AfectoIGV", DbType.String, pbe.AfectoIGV);
                sqlDB.AddInParameter(dbCmd, "@as_RetencionIGV", DbType.String, pbe.RetencionIGV);
                sqlDB.AddInParameter(dbCmd, "@as_CreditoFiscal", DbType.String, pbe.CreditoFiscal);
                sqlDB.AddInParameter(dbCmd, "@as_RTPS", DbType.String, pbe.RTPS);
                sqlDB.AddInParameter(dbCmd, "@as_Logo", DbType.String, pbe.Logo);
                sqlDB.AddInParameter(dbCmd, "@as_RepresentanteLegal", DbType.String, pbe.RepresentanteLegal);
                sqlDB.AddInParameter(dbCmd, "@as_RepresentanteLegalDocumento", DbType.String, pbe.RepresentanteLegalDocumento);
                sqlDB.AddInParameter(dbCmd, "@as_TipoCompania", DbType.String, pbe.TipoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_RangoIngresos", DbType.String, pbe.RangoIngresos);
                sqlDB.AddInParameter(dbCmd, "@as_RangoPersonal", DbType.String, pbe.RangoPersonal);
                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                sqlDB.AddInParameter(dbCmd, "@as_UsuarioModificacion", DbType.String, pbe.UsuarioModificacion);
                sqlDB.AddInParameter(dbCmd, "@as_General", DbType.String, pbe.General);
                sqlDB.AddInParameter(dbCmd, "@ai_IdPersonaLegal", DbType.Int32, pbe.IdPersonaLegal);
                sqlDB.AddInParameter(dbCmd, "@ai_IdPaisRegistroPersona", DbType.Int32, pbe.IdPaisRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@ai_IdDepartamentoRegistroPersona", DbType.Int32, pbe.IdDepartamentoRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@ai_IdProvinciaRegistroPersona", DbType.Int32, pbe.IdProvinciaRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@at_FechaRegistroPersona", DbType.DateTime, pbe.FechaRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@as_TomoRegistroPersona", DbType.String, pbe.TomoRegistroPersona);
                sqlDB.AddInParameter(dbCmd, "@at_FechaRegistro", DbType.DateTime, pbe.FechaRegistro);
                sqlDB.AddInParameter(dbCmd, "@ai_IdPaisRegistro", DbType.Int32, pbe.IdPaisRegistro);
                sqlDB.AddInParameter(dbCmd, "@ai_IdDepartamentoRegistro", DbType.Int32, pbe.IdDepartamentoRegistro);
                sqlDB.AddInParameter(dbCmd, "@ai_IdProvinciaRegistro", DbType.Int32, pbe.IdProvinciaRegistro);
                sqlDB.AddInParameter(dbCmd, "@as_TomoRegistroLegal", DbType.String, pbe.TomoRegistroLegal);
                sqlDB.AddInParameter(dbCmd, "@as_Nemonico", DbType.String, pbe.Nemonico);
                sqlDB.AddInParameter(dbCmd, "@aim_ImagenLogo", DbType.Binary, pbe.ImagenLogo);


                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<Int32>(reader, "IdCompania");
                        be.FechaModificacion = DBValue.Get<DateTime>(reader, "FechaModificacion");
                        //Error
                        be.Error = DBValue.Get<Int32>(reader, "CodigoError");
                        be.Mensaje = DBValue.Get<String>(reader, "MensajeError");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            finally
            {
                dbCmd.Dispose();
            }
            return be;
        }

        /// <summary>
        /// Método Actualiza Spring 
        /// </summary>
        /// <returns>Devuelve una entidad</returns>
        public BECompania ActualizarSpring(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Actualizar_Spring");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
                sqlDB.AddInParameter(dbCmd, "@ai_IdCompania", DbType.Int32, pbe.IdCompania);
                sqlDB.AddInParameter(dbCmd, "@ai_IdPersona", DbType.Int32, pbe.IdSpring);
                sqlDB.AddInParameter(dbCmd, "@as_CodigoCompania", DbType.String, pbe.CodigoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Descripcion", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_DescripcionLarga", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_PaginaWeb", DbType.String, pbe.PaginaWeb);
                sqlDB.AddInParameter(dbCmd, "@as_AfectoIGV", DbType.String, pbe.AfectoIGV);
                sqlDB.AddInParameter(dbCmd, "@as_RetencionIGV", DbType.String, pbe.RetencionIGV);
                sqlDB.AddInParameter(dbCmd, "@as_CreditoFiscal", DbType.String, pbe.CreditoFiscal);
                sqlDB.AddInParameter(dbCmd, "@as_RTPS", DbType.String, pbe.RTPS);
                sqlDB.AddInParameter(dbCmd, "@as_RepresentanteLegal", DbType.String, pbe.RepresentanteLegal);
                sqlDB.AddInParameter(dbCmd, "@as_RepresentanteLegalDocumento", DbType.String, pbe.RepresentanteLegalDocumento);
                sqlDB.AddInParameter(dbCmd, "@as_TipoEmpresa", DbType.String, pbe.TipoCompania);
                sqlDB.AddInParameter(dbCmd, "@at_FechaRegistro", DbType.DateTime, pbe.FechaRegistro);
                sqlDB.AddInParameter(dbCmd, "@as_TomoRegistroLegal", DbType.String, pbe.TomoRegistroLegal);
                sqlDB.AddInParameter(dbCmd, "@as_Direccion", DbType.String, pbe.DescripcionDireccion );
                sqlDB.AddInParameter(dbCmd, "@as_DireccionReferencia", DbType.String, pbe.DireccionReferencia);
                sqlDB.AddInParameter(dbCmd, "@as_Documento", DbType.String, pbe.Documento);
                sqlDB.AddInParameter(dbCmd, "@as_Telefono1", DbType.String, pbe.Telefono1);
                sqlDB.AddInParameter(dbCmd, "@as_Telefono2", DbType.String, pbe.Telefono2);
                sqlDB.AddInParameter(dbCmd, "@as_Telefono3", DbType.String, pbe.Telefono3);
                sqlDB.AddInParameter(dbCmd, "@as_Fax1", DbType.String, pbe.Telefono3);
                sqlDB.AddInParameter(dbCmd, "@as_Nacionalidad", DbType.String, pbe.Nacionalidad);
                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                sqlDB.AddInParameter(dbCmd, "@as_UsuarioModificacion", DbType.String, pbe.UsuarioModificacion);

                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<Int32>(reader, "IdCompania");
                        be.FechaModificacion = DBValue.Get<DateTime>(reader, "FechaModificacion");
                        //Error
                        be.Error = DBValue.Get<Int32>(reader, "CodigoError");
                        be.Mensaje = DBValue.Get<String>(reader, "MensajeError");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            finally
            {
                dbCmd.Dispose();
            }
            return be;
        }


        /// <summary>
        /// Método Actualiza Spring 
        /// </summary>
        /// <returns>Devuelve una entidad</returns>
        public BECompania ActualizarOwnerSpring(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Actualizar_Owner_Spring");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
                sqlDB.AddInParameter(dbCmd, "@ai_IdCompania", DbType.Int32, pbe.IdCompania);
                sqlDB.AddInParameter(dbCmd, "@as_CodigoCompania", DbType.String, pbe.CodigoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Descripcion", DbType.String, pbe.Descripcion);
                sqlDB.AddInParameter(dbCmd, "@as_UsuarioModificacion", DbType.String, pbe.UsuarioModificacion);

                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<Int32>(reader, "IdCompania");
                        be.FechaModificacion = DBValue.Get<DateTime>(reader, "FechaModificacion");
                        //Error
                        be.Error = DBValue.Get<Int32>(reader, "CodigoError");
                        be.Mensaje = DBValue.Get<String>(reader, "MensajeError");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            finally
            {
                dbCmd.Dispose();
            }
            return be;
        }

        /// <summary>
        /// Método Actualiza Rangos
        /// </summary>
        /// <returns>Devuelve una entidad</returns>
        public BECompania Actualizar_Rangos(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Actualizar_Rangos");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;

                sqlDB.AddInParameter(dbCmd, "@ai_IdCompania", DbType.Int32, pbe.IdCompania);
                sqlDB.AddInParameter(dbCmd, "@as_PaginaWeb", DbType.String, pbe.PaginaWeb);
                sqlDB.AddInParameter(dbCmd, "@as_RepresentanteLegal", DbType.String, pbe.RepresentanteLegal);
                sqlDB.AddInParameter(dbCmd, "@as_RangoIngresos", DbType.String, pbe.RangoIngresos);
                sqlDB.AddInParameter(dbCmd, "@as_RangoPersonal", DbType.String, pbe.RangoPersonal);

                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                sqlDB.AddInParameter(dbCmd, "@as_UsuarioModificacion", DbType.String, pbe.UsuarioModificacion);

                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<Int32>(reader, "IdCompania");
                        be.FechaModificacion = DBValue.Get<DateTime>(reader, "FechaModificacion");
                        //Error
                        be.Error = DBValue.Get<Int32>(reader, "CodigoError");
                        be.Mensaje = DBValue.Get<String>(reader, "MensajeError");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            finally
            {
                dbCmd.Dispose();
            }
            return be;
        }


        /// <summary>
        /// Método Eliminar
        /// </summary>
        /// <returns>Devuelve una entidad</returns>
        public BECompania Eliminar(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Eliminar");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
                sqlDB.AddInParameter(dbCmd, "@ai_IdCompania", DbType.Int32, pbe.IdCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                sqlDB.AddInParameter(dbCmd, "@as_UsuarioModificacion", DbType.String, pbe.UsuarioModificacion);

                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<Int32>(reader, "IdCompania");
                        be.FechaModificacion = DBValue.Get<DateTime>(reader, "FechaModificacion");
                        //Error
                        be.Error = DBValue.Get<Int32>(reader, "CodigoError");
                        be.Mensaje = DBValue.Get<String>(reader, "MensajeError");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            finally
            {
                dbCmd.Dispose();
            }
            return be;
        }

        /// <summary>
        /// Método Eliminar Spring
        /// </summary>
        /// <returns>Devuelve una entidad</returns>
        public BECompania EliminarSpring(BECompania pbe)
        {
            BECompania be = new BECompania();
            SqlDatabase sqlDB = new SqlDatabase(CadenaConexion.Obtener());
            DbCommand dbCmd = sqlDB.GetStoredProcCommand("uSP_INSIFO_GEN_Compania_Eliminar_Spring");

            try
            {
                dbCmd.CommandTimeout = CadenaConexion.CommandTimeout;
                sqlDB.AddInParameter(dbCmd, "@ai_IdCompania", DbType.Int32, pbe.IdCompania);
                sqlDB.AddInParameter(dbCmd, "@as_CodigoCompania", DbType.String, pbe.CodigoCompania);
                sqlDB.AddInParameter(dbCmd, "@as_Estado", DbType.String, pbe.Estado);
                sqlDB.AddInParameter(dbCmd, "@as_UsuarioModificacion", DbType.String, pbe.UsuarioModificacion);

                using (IDataReader reader = sqlDB.ExecuteReader(dbCmd))
                {
                    while (reader.Read())
                    {
                        be.IdCompania = DBValue.Get<Int32>(reader, "IdCompania");
                        be.FechaModificacion = DBValue.Get<DateTime>(reader, "FechaModificacion");
                        //Error
                        be.Error = DBValue.Get<Int32>(reader, "CodigoError");
                        be.Mensaje = DBValue.Get<String>(reader, "MensajeError");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            finally
            {
                dbCmd.Dispose();
            }
            return be;
        }

        #endregion  Members
    }
}
